package com.example.musicplayer.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.example.musicplayer.R;
import com.example.musicplayer.musicClass.MusicInfo;
import com.example.musicplayer.service.MusicService;
import com.example.musicplayer.settings.DesktopLyricSettings;

public class DesktopMusicListAdapter extends SlideListViewAdapter {
    private final MusicService.MusicBinder mMusicBinder;
    private final DesktopLyricSettings mLyricSettings;

    public DesktopMusicListAdapter(ListView listView, MusicService.MusicBinder musicBinder
            , DesktopLyricSettings lyricSettings, Context context) {
        super(context, listView);
        mMusicBinder = musicBinder;
        mLyricSettings = lyricSettings;
    }

    @Override
    public int getCount() {
        if (mMusicBinder != null) {
            return mMusicBinder.getMusicCounts();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mMusicBinder != null) {
            return mMusicBinder.getMusicInfo(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    protected View bindView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.desktop_music_list_item, null);
            viewHolder.mInfo = convertView.findViewById(R.id.music_info);
            viewHolder.mInfo1 = convertView.findViewById(R.id.music_info_1);
            viewHolder.mDelete = convertView.findViewById(R.id.music_delete);
            //set stroke for text
            TextPaint paint = viewHolder.mInfo.getPaint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(1.0f);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (mMusicBinder != null) {
            MusicInfo currentMusic = mMusicBinder.getMusicInfo(position);
            if (currentMusic != null) {
                String info = currentMusic.getTitle() + " - " + currentMusic.getArtist();
                viewHolder.mInfo.setText(info);
                viewHolder.mInfo1.setText(info);
            }
            if (position == mMusicBinder.getNowMusicPosition()) {
                viewHolder.mInfo1.setTextColor(mLyricSettings.getLyricTextColor());
            } else {
                viewHolder.mInfo1.setTextColor(mContext.getResources().getColor(R.color.desktop_lyric_normal));
            }
            viewHolder.mDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mMusicBinder.deleteOneMusic(position);
                    notifyDataSetChanged();
                }
            });
        }
        return convertView;
    }

    private static class ViewHolder {
        TextView mInfo;
        TextView mInfo1;
        View mDelete;
    }
}